package com.example.weather;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private TextView txtTile;
    private TextView txtCity;
    private TextView tWeather;
    private TextView tWendu;
    private TextView tShidu;
    private TextView tShengshi;
    private TextView tFengsu;
    private ImageView imgTianqiJpg;
    private TextView tTitle;
    private TextView tUpdatatime;
    private Button btSy;
    private Button btNb;
    private Button btSx;
    private Button btBeijing;
    private Spinner sp;
    private String str;
    private String citycode;
  private  TextView ed_selectcitycode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtTile = (TextView) findViewById(R.id.txt_tile);
        txtCity = (TextView) findViewById(R.id.txt_city);
        tWeather = (TextView) findViewById(R.id.t_weather);
        tWendu = (TextView) findViewById(R.id.t_wendu);
        tShidu = (TextView) findViewById(R.id.t_shidu);
        tShengshi = (TextView) findViewById(R.id.t_shengshi);
        tFengsu = (TextView) findViewById(R.id.t_fengsu);
        imgTianqiJpg = (ImageView) findViewById(R.id.img_tianqi_jpg);
        tTitle = (TextView) findViewById(R.id.t_title);
        tUpdatatime = (TextView) findViewById(R.id.t_updatatime);
        btSy = (Button) findViewById(R.id.bt_sy);
        btNb = (Button) findViewById(R.id.bt_nb);
        btSx = (Button) findViewById(R.id.bt_sx);
        btBeijing = (Button) findViewById(R.id.bt_beijing);
        ed_selectcitycode=(TextView)findViewById(R.id.ed_citycodeselect);
        Weather weather=new Weather();
        try{

            weather.execute(new URL("https://api.help.bj.cn/apis/weather/?id=101210401"));
        }
        catch (MalformedURLException e){
            e.printStackTrace();
        }
        //点击四个按钮跳到对应城市的界面
        btSy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, JieMain1.class);//this前面为当前activty名称，class前面为要跳转到得activity名称
                startActivity(intent);
            }
        });
        btSx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, JieMain3.class);//this前面为当前activty名称，class前面为要跳转到得activity名称
                startActivity(intent);
            }
        });
        btNb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, JieMain2.class);//this前面为当前activty名称，class前面为要跳转到得activity名称
                startActivity(intent);
            }
        });

        btBeijing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, JieMain4.class);//this前面为当前activty名称，class前面为要跳转到得activity名称
                startActivity(intent);
            }
        });
        //将spinner里面的取前九位及城市编号然后传递给citycode
        sp = (Spinner) findViewById(R.id.spinner);
        str = (String) sp.getSelectedItem();
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {

                //拿到被选择项的值
                str = (String) sp.getSelectedItem();
                //把该值的前9位传给 citydode
                citycode=str.substring(0,9);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });
    }
//用spinner中获取到对应城市的编号查询天气的信息
    public void qingqiu(View view) {

        txtTile.setText("今日天气");
        try{
            Weather weather=new Weather();
            weather.execute(new URL("https://api.help.bj.cn/apis/weather/?id="+citycode));
        }
        catch (MalformedURLException e){
            e.printStackTrace();
        }

    }

    public void ningbo(View view) {

    }
//用城市编号查询对应城市天的信息
    public void selectcodecity(View view) {
        txtTile.setText("今日天气");
        try{
            Weather weather=new Weather();
            weather.execute(new URL("https://api.help.bj.cn/apis/weather/?id="+ed_selectcitycode.getText().toString()));
        }
        catch (MalformedURLException e){
            e.printStackTrace();
        }
    }

    class Weather extends AsyncTask<URL,Integer,String> {
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            WeatherBean weatherBean= JSON.parseObject(s,WeatherBean.class);
            //在JSON中获取的值对应的输出在文本框
            txtCity.setText(weatherBean.getCity());
            tUpdatatime.setText(weatherBean.getUptime());
            tTitle.setText(weatherBean.getToday());
            tWendu.setText(weatherBean.getTemp()+"   ℃");
            tShidu.setText(weatherBean.getHumidity());
            tShengshi.setText(weatherBean.getCity());
            tFengsu.setText(weatherBean.getWdspd());
            tWeather.setText(weatherBean.getWeather());
            //将天气文字转换为对应图片
            if(weatherBean.getWeather()!=null){
                if(weatherBean.getWeather().equals("晴")){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.qing);
                }
                if(weatherBean.getWeather().equals("多云")){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.duoyun);
                }
                if(weatherBean.getWeather().equals("阴")){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.ying);
                }
                //判断天气情况中是否含有该关键字
                if(weatherBean.getWeather().indexOf("雨")!=-1){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.yu);
                }
                if(weatherBean.getWeather().indexOf("霾")!=-1){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.mai);
                }
                if(weatherBean.getWeather().indexOf("雾")!=-1){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.wu);
                }
                if(weatherBean.getWeather().indexOf("雪")!=-1){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.xue);
                }
            }else {
                Log.d("MainActivity","未找到该天气");
                imgTianqiJpg.setBackgroundResource(R.drawable.yuanshi);
            }



        }

        @Override
        protected String doInBackground(URL... urls) {
            String s=HttpGetData(urls[0]);
            Log.d("MainActivity","获得天气数据"+s);

            return s;
        }
    }

    //这个方法将执行
    public String HttpGetData(URL url){
        String s=null;
        try{  //1。网络连接创建
            HttpURLConnection httpURLConnection=  (HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.connect();
            //2.发起连接
            if(httpURLConnection.getResponseCode()==200){
                InputStream inputStream=httpURLConnection.getInputStream();
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                s =bufferedReader.readLine();
            }}
        catch (Exception e){
            e.printStackTrace();
        }
        return s;
    }

}
